class Solution {
public:
    int maxFrequency(vector<int>& nums, int k, int numOperations) {
        unordered_map<int, int> count;
        map<int, int> diff;
        for (int num : nums) {
            count[num]++;
            diff[num];
            diff[num - k]++;
            diff[num + k + 1]--;
        }
        int res = 0, sum = 0;
        for (auto& [x, y] : diff) {
            sum += y;
            res = max(res, min(numOperations + count[x], sum));
        }
        return res;
    }
};